#include "log.h"

/*
 * 
 * name: Log
 * Constructor
 */
Log::Log(){
  m_bDebugMode=true;
  m_bSpecialDebugMode=true;
  m_bErrorMode=true;
  m_bInfoMode=true;
  m_bFileMode=false;
  m_bColors=false;
}

/*
 * 
 * name: ~Log
 * Destructor
 */
Log::~Log(){
}

void Log::freeMemory()
{
	if(m_pOutputFile)fclose(m_pOutputFile);
}
/*
 * 
 * name: debug
 * Prints a Debug message.
 * @param _string: Debug message to show
 * @return voif
 */
void Log::debug(const char * _string, ...)
{ 
  if(!m_bDebugMode)
    return;
  
  char *finalString=(char *)malloc(512);
  va_list vl;
  va_start(vl, _string);
  
  time_t t=time(NULL);
  char timeString[80];
  strftime( timeString, 80, "[%D %H:%M.%S]", localtime(&t) );
  
  if(m_bColors){
    sprintf(finalString,"\033[01;30m%s DEBUG\t%s\x1b[m\n",timeString,_string);
    vprintf(finalString,vl);
  }
  else{
    sprintf(finalString,"%s DEBUG\t%s\n",timeString,_string);
    vprintf(finalString,vl);
  }  
  va_end(vl);
  free(finalString);
}


/*
 * 
 * name: info
 * Prints an Info message.
 * @param _string: Debug message to show
 * @return voif
 */
void Log::info(const char * _string, ...)
{ 
  if(!m_bInfoMode)
    return;
  
  char *finalString=(char *)malloc(512);
  va_list vl;
  va_start(vl, _string);

  time_t t=time(NULL);
  char timeString[80];
  strftime( timeString, 80, "[%D %H:%M.%S]", localtime(&t) );
  
  sprintf(finalString,"%s INFO\t%s\n",timeString,_string);
  vprintf(finalString,vl);
  va_end(vl);
  free(finalString);
}

/*
 * 
 * name: error
 * Prints an Error message.
 * @param _string: Message to show
 * @return void
 */
void Log::error(const char * _string, ...)
{ 
  if(!m_bErrorMode)
    return;
  
  char *finalString=(char *)malloc(512);
  va_list vl;
  va_start(vl, _string);
  
  time_t t=time(NULL);
  char timeString[80];
  strftime( timeString, 80, "[%D %H:%M.%S]", localtime(&t) );
  
  if(m_bColors)
  {
    sprintf(finalString,"\x1b[91m%s ERROR\t%s\x1b[m\n",timeString,_string);
    vprintf(finalString,vl);
  }
  else
  {
    sprintf(finalString,"%s ERROR\t%s\n",timeString,_string);
    vprintf(finalString,vl);
  }  
  va_end(vl);
  free(finalString);
}

/*
 * 
 * name: getInstance
 * Returns a instance of Clog. This is because Clog is a singleton class.
 * @param N/A
 * @return Log&: Instance to this unique class
 */
Log& Log::getInstance()
{
  static Log log;
  return log;
}


